package com.zyao89.view.zloading.rect;

import com.zyao89.view.zloading.base.BaseStateBuilder;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Created by zyao89 on 2018/3/17.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: https://zyao89.cn
 */
public class ChartRectBuilder extends BaseStateBuilder {
    /**
     * 总数
     */
    private final int SUM_NUM = 5;
    /**
     * 动画间隔时长
     */
    private volatile long mAnimateDurationTime = 500;
    /**
     * 当前
     */
    private volatile int mCurrStateNum = 0;
    private volatile float mCurrAnimatedValue = 0;
    private Paint mPaint;
    private float mR;
    private RectFloat mStairRectF;


    @Override
    protected int getStateCount() {
        return SUM_NUM + 1;
    }

    @Override
    protected void initParams(Context context, Paint paint) {
        mPaint = paint;
        mPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        mR = getAllSize();
        mStairRectF = new RectFloat();
    }

    @Override
    protected void onComputeUpdateValue(AnimatorValue animation, float animatedValue, int state) {
        mCurrStateNum = state;
        mCurrAnimatedValue = animatedValue;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // 高度
        float floorHeight = mR * 2 / SUM_NUM;
        float space = floorHeight * 0.5f;
        // 起点
        float startXP = NumCalcUtil.subtract(getViewCenterX(), mR);
        float startYP = NumCalcUtil.add(getViewCenterY(), mR);

        // 清
        mStairRectF.clear();
        for (int i = 0; i < SUM_NUM; i++) {
            if (i > mCurrStateNum) { // 限制层
                break;
            }
            float offsetHV = NumCalcUtil.subtract(0.5f, Math.abs(NumCalcUtil.subtract(mCurrAnimatedValue, 0.5f))) * floorHeight;
            int j = i % 3;
            if (i == mCurrStateNum) { // 当前
                mStairRectF.modify(NumCalcUtil.add(startXP, i * floorHeight),
                        NumCalcUtil.subtract(startYP, (j + 1) * floorHeight * mCurrAnimatedValue),
                        NumCalcUtil.subtract(NumCalcUtil.add(startXP, (i + 1) * floorHeight), space), startYP);
            } else {
                mStairRectF.modify(NumCalcUtil.add(startXP, i * floorHeight),
                        NumCalcUtil.subtract(NumCalcUtil.subtract(startYP, (j + 1) * floorHeight), offsetHV),
                        NumCalcUtil.subtract(NumCalcUtil.add(startXP, (i + 1) * floorHeight), space), startYP);
            }
            canvas.drawRect(mStairRectF, mPaint);
        }
    }

    @Override
    protected void prepareStart(AnimatorValue animation) {
        mAnimateDurationTime = ceil(getAnimationDuration() * 0.4);

        // 动画间隔
        animation.setDuration(mAnimateDurationTime);
    }

    @Override
    protected void prepareEnd() {
        mCurrStateNum = 0;
        mCurrAnimatedValue = 0;
    }
}
